{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "frank-quebec",
   "metadata": {},
   "source": [
    "## 1 介绍"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "correct-cable",
   "metadata": {},
   "source": [
    "## 2 模型训练"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "classified-vitamin",
   "metadata": {},
   "source": [
    "### 2.0 准备阶段\n",
    "\n",
    "#### 2.0.1 导入必要的包和库"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "attempted-penetration",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import pickle\n",
    "import xgboost as xgb\n",
    "import lightgbm as lgb\n",
    "\n",
    "import warnings\n",
    "warnings.filterwarnings(\"ignore\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "acknowledged-bulgaria",
   "metadata": {},
   "source": [
    "#### 2.0.2 定义一些基本函数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "understanding-footwear",
   "metadata": {},
   "source": [
    "### 2.1 训练过程"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "planned-wheel",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "[LightGBM] [Info] Number of positive: 625, number of negative: 29764\n[LightGBM] [Warning] Auto-choosing row-wise multi-threading, the overhead of testing was 0.003973 seconds.\nYou can set `force_row_wise=true` to remove the overhead.\nAnd if memory is not enough, you can set `force_col_wise=true`.\n[LightGBM] [Info] Total Bins 8652\n[LightGBM] [Info] Number of data points in the train set: 30389, number of used features: 95\n[LightGBM] [Info] [binary:BoostFromScore]: pavg=0.020567 -> initscore=-3.863303\n[LightGBM] [Info] Start training from score -3.863303\n"
    },
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "<lightgbm.basic.Booster at 0x7fb4613b6198>"
     },
     "metadata": {},
     "execution_count": 2
    }
   ],
   "source": [
    "datapath = '../../produce/featureData.csv'\n",
    "data = pd.read_csv(datapath ,sep=' ')\n",
    "\n",
    "filename = '../../produce/serialize_constant'\n",
    "\n",
    "with open(filename, 'rb') as f:  \n",
    "    serialize_constant = pickle.load(f)\n",
    "    trainLen = serialize_constant['trainLen']\n",
    "    trainlabel = serialize_constant['trainlabel']\n",
    "    testInstanceID = serialize_constant['testInstanceID']\n",
    "\n",
    "data = data.iloc[0 : trainLen, : ]\n",
    "target = trainlabel\n",
    "\n",
    "# 可以选择的模型包括 'LGBMmodel' 'XGBModel'\n",
    "# XGBModel\n",
    "dtrain = xgb.DMatrix(data=data.values, label=target.values)\n",
    "progress = dict()\n",
    "# xgbparamSetting()\n",
    "param = {\n",
    "            'learning_rate': 0.05,\n",
    "            'eta': 0.4,\n",
    "            'max_depth': 3,\n",
    "            'gamma': 0,\n",
    "            'subsample': 0.8,\n",
    "            'colsample_bytree': 0.8,\n",
    "            'alpha': 1,\n",
    "            # 'lambda' : 0.1,\n",
    "            'nthread': 4,\n",
    "            'objective': 'binary:logistic',\n",
    "            'eval_metric': 'logloss'\n",
    "        }\n",
    "# XGBbestNumRounds\n",
    "num_round = 811\n",
    "bst = xgb.train(param, dtrain, num_round, evals_result=progress)\n",
    "bst.save_model('../../produce/xgbModelFinal')\n",
    "\n",
    "# LGBMmodel\n",
    "dtrain = lgb.Dataset(data=data.values, label=target.values)\n",
    "progress = dict()\n",
    "# LGBMparamSetting()\n",
    "param = {\n",
    "            'learning_rate': 0.01,\n",
    "            'num_leaves': 32,\n",
    "            # 'eta' : 0.4,\n",
    "            'subsample': 0.35,\n",
    "            'colsample_bytree': 0.3,\n",
    "            'nthread': 4,\n",
    "            # 'lambda_l1' : 0.1,\n",
    "            'objective': 'binary',\n",
    "            'metric': 'binary_logloss'\n",
    "        }\n",
    "# LGBMbestNumRounds\n",
    "num_round = 2263\n",
    "bst = lgb.train(param, dtrain, num_round, evals_result=progress)\n",
    "bst.save_model('../../produce/lgbModelFinal')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "racial-director",
   "metadata": {},
   "source": [
    "### 2.2 测试过程"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "fifty-zealand",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "48757\n30389\n(30389, 28)\n(18371, 26)\n0        2475218615076601065\n1         398316874173557226\n2        6586402638209028583\n3        1040996105851528465\n4        6316278569655873454\n                ...         \n18366     196556205576680664\n18367    3972597272081581867\n18368    7331170863335915445\n18369    4801514605649495171\n18370    4931599763172137858\nName: instance_id, Length: 18371, dtype: int64\n18371\n"
    }
   ],
   "source": [
    "datapath = '../../produce/featureData.csv'\n",
    "data = pd.read_csv(datapath ,sep=' ')\n",
    "print(data['instance_id'].nunique())\n",
    "\n",
    "data = data.iloc[trainLen : , : ] # 训练集后应该是测试集\n",
    "\n",
    "# # round1\n",
    "# train_path_people_1='../../datasets/cut/round1/round1_train_cut_by_people.txt'\n",
    "# train_path_type_1='../../datasets/cut/round1/round1_train_cut_by_type.txt'\n",
    "# test_path_a_1='../../datasets/cut/round1/round1_ijcai_18_test_a_20180301.txt'\n",
    "# test_path_b_1='../../datasets/cut/round1/round1_ijcai_18_test_b_20180418.txt'\n",
    "# # round2\n",
    "# train_path_type_2='../../datasets/cut/round2/round2_train_cut_by_type.txt'\n",
    "# test_path_a_2='../../datasets/cut/round2/round2_test_a.txt'\n",
    "# test_path_b_2='../../datasets/cut/round2/round2_test_b.txt'\n",
    "# train=pd.read_table(train_path_type_1,delimiter=' ')\n",
    "# test=pd.read_table(test_path_a_1,delimiter=' ')\n",
    "# print(trainLen)\n",
    "# print(train.shape)\n",
    "# print(test.shape)\n",
    "\n",
    "writefileName = '../../produce/result.csv'\n",
    "\n",
    "XGBmodel = xgb.Booster(model_file='../../produce/xgbModelFinal')\n",
    "XGBpreds = XGBmodel.predict(xgb.DMatrix(data.values))\n",
    "LGBMmodel = lgb.Booster(model_file='../../produce/lgbModelFinal')\n",
    "LGBMpreds = LGBMmodel.predict(data.values)\n",
    "\n",
    "preds = 0.5 * XGBpreds + 0.5 * LGBMpreds\n",
    "\n",
    "sub = pd.DataFrame()\n",
    "print(testInstanceID)\n",
    "print(len(preds))\n",
    "sub['instance_id'] = testInstanceID\n",
    "sub['predicted_score'] = preds # 已经解决实际预测数和测试集数量不一致问题：这是由于特征集加入时候导致的\n",
    "\n",
    "sub.to_csv(writefileName, sep=\" \", index=False, line_terminator='\\r')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.6.12 64-bit ('tc': conda)",
   "language": "python",
   "name": "python_defaultSpec_1613703465607"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.12-final"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}